﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

using EHTSaleManager.Code;
using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Utility;
using EHTSaleManager.Code.Entity;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminCustomers : System.Web.UI.Page
    {
        private Int32 pageIndex = 1;
        private Int32 pageSize = 5;
        private Int32 totalRecord;

        private Int32 approved = -1;
        private Int32 levelService = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch (Exception)
            {
                pageIndex = 1;
            }

            try
            {
                approved = Int32.Parse(Request.QueryString["Approved"].ToString());
            }
            catch (Exception)
            {
                approved = -1;
            }

            try
            {
                levelService = Int32.Parse(Request.QueryString["LevelServiceID"].ToString());
            }
            catch (Exception)
            {
                levelService = -1;
            }

            pageSize = PaggingControl1.PageSize;

            if (!Page.IsPostBack)
            {
                DataSet dsLevelServices = ServiceLevelsProvider.GetServiceLevels();
                this.ddlServiceLevel.DataValueField = "SeviceLevelID";
                this.ddlServiceLevel.DataTextField = "Description";
                ddlServiceLevel.DataSource = dsLevelServices;
                ddlServiceLevel.DataBind();
                ddlServiceLevel.Items.Insert(0, new ListItem("-- Alle Kunder --", "-1"));
                
                ddlServiceLevel.SelectedValue = levelService.ToString();

                rpCustomers.DataSource = GetCustomerByRoleAndIsApproved(HangSo.ROLE_CUSTOMER, approved, -1);
                rpCustomers.DataBind();

                


            }
        }

        private MembershipUserCollection GetCustomerByRoleAndIsApproved(String roleName, Int32 approved, Int32 levelService)
        {
            MembershipUserCollection customerList = new MembershipUserCollection();
            MembershipUserCollection customerListPagging = new MembershipUserCollection();
            MembershipUserCollection allUser = Membership.GetAllUsers(); // lấy tất cả User

            if (roleName != "all")
            {
                // chọn các user thuộc nhóm Administrator
                foreach (MembershipUser theUser in allUser)
                {
                    // lọc theo tiêu chí Approved
                    if (Roles.IsUserInRole(theUser.UserName, roleName) && (theUser.IsApproved == false || approved == -1))
                    {
                        ProfileBase theProfile = ProfileBase.Create(theUser.UserName);
                        Int32 level = Int32.Parse(theProfile.GetPropertyValue("ServiceLevelID").ToString());

                        // lọc theo tiêu chí LevelService
                        if (level == levelService || levelService == -1)
                            customerList.Add(theUser);
                    }
                }
            }

            //thực hiện pagging
            Int32 pageLowerBound = pageSize * (pageIndex - 1) + 1;
            Int32 pageUpperBound = pageSize + pageLowerBound - 1;
            totalRecord = customerList.Count;
            Int32 index = 1;

            foreach (MembershipUser theUser in customerList)
            {
                if (index >= pageLowerBound && index <= pageUpperBound)
                {
                    customerListPagging.Add(theUser);
                }

                index += 1;
            }

            this.PaggingControl1.PageURL = "AdminCustomers.aspx?Approved=" + approved.ToString() + "&LevelService=" + levelService.ToString();
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PagingSize = pageSize;

            return customerListPagging;
        }
        

        protected void ddlServiceLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            levelService = Int32.Parse(this.ddlServiceLevel.SelectedValue);

            rpCustomers.DataSource = GetCustomerByRoleAndIsApproved(HangSo.ROLE_CUSTOMER, approved, levelService);
            rpCustomers.DataBind();
        }
    }
}